package com.party.core.service.crowdfund.impl;

import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.core.dao.read.crowdfund.CrowdfundRankReadDao;
import com.party.core.dao.write.crowdfund.CrowdfundRankWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.activity.Activity;
import com.party.core.model.crowdfund.CrowdfundRank;
import com.party.core.model.crowdfund.Project;
import com.party.core.model.crowdfund.Support;
import com.party.core.model.crowdfund.TargetProject;
import com.party.core.service.crowdfund.ICrowdfundRankService;
import com.sun.istack.NotNull;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @Author: Administrator.
 * @Description: 众筹排名
 * @Date:Created in 2017/9/26 0026 上午 9:48.
 */
@Service
public class CrowdfundRankService implements ICrowdfundRankService {
    @Autowired
    private CrowdfundRankReadDao crowdfundRankReadDao;
    @Autowired
    private CrowdfundRankWriteDao crowdfundRankWriteDao;

    /**
     * 众筹排名插入
     *
     * @param crowdfundRank 众筹排名插入
     * @return 编号
     */
    @Override
    public String insert(CrowdfundRank crowdfundRank) {
        BaseModel.preInsert(crowdfundRank);
        boolean result = crowdfundRankWriteDao.insert(crowdfundRank);
        if (result) {
            return crowdfundRank.getId();
        }
        return null;
    }

    /**
     * 更新商品众筹排名
     *
     * @param crowdfundRank 商品众筹排名
     * @return 更新结果（true/false）
     */
    @Override
    public boolean update(CrowdfundRank crowdfundRank) {
        crowdfundRank.setUpdateDate(new Date());
        return crowdfundRankWriteDao.update(crowdfundRank);
    }

    /**
     * 逻辑删除
     *
     * @param id 实体主键
     * @return 删除结果（true/false）
     */
    @Override
    public boolean deleteLogic(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return crowdfundRankWriteDao.deleteLogic(id);
    }

    /**
     * 物理删除
     *
     * @param id 实体主键
     * @return 删除结果（true/false）
     */
    @Override
    public boolean delete(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return crowdfundRankWriteDao.delete(id);
    }

    /**
     * 批量逻辑删号
     *
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    @Override
    public boolean batchDeleteLogic(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return crowdfundRankWriteDao.batchDeleteLogic(ids);
    }

    /**
     * 批量物理删除
     *
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    @Override
    public boolean batchDelete(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return crowdfundRankWriteDao.batchDelete(ids);
    }

    /**
     * 获取众筹排名
     *
     * @param id 主键
     * @return 众筹排名
     */
    @Override
    public CrowdfundRank get(String id) {
        return crowdfundRankReadDao.get(id);
    }

    /**
     * 分页查询
     *
     * @param crowdfundRank 众筹排名
     * @param page          分页信息
     * @return 分页列表
     */
    @Override
    public List<CrowdfundRank> listPage(CrowdfundRank crowdfundRank, Page page) {
        return crowdfundRankReadDao.listPage(crowdfundRank, page);
    }

    /**
     * 查询所有
     *
     * @param crowdfundRank 众筹排名
     * @return 分页列表
     */
    @Override
    public List<CrowdfundRank> list(CrowdfundRank crowdfundRank) {
        return crowdfundRankReadDao.listPage(crowdfundRank, null);
    }

    @Override
    public List<CrowdfundRank> batchList(@NotNull Set<String> ids, CrowdfundRank crowdfundRank, Page page) {
        return crowdfundRankReadDao.batchList(ids, new HashMap<String, Object>(), page);
    }

    @Override
    public CrowdfundRank getCrowdRank(CrowdfundRank rank, int percent) {
        return crowdfundRankReadDao.getCrowdRank(rank, percent);
    }

    @Override
    public List<CrowdfundRank> findByEventId(String eventId) {
        CrowdfundRank crowdfundRank = new CrowdfundRank();
        crowdfundRank.setEventId(eventId);
        return crowdfundRankReadDao.listPage(crowdfundRank, null);
    }

    @Override
    public List<CrowdfundRank> findByTargetId(String targetId) {
        CrowdfundRank crowdfundRank = new CrowdfundRank();
        crowdfundRank.setTargetId(targetId);
        return crowdfundRankReadDao.listPage(crowdfundRank, null);
    }

    @Override
    public void insertCrowdRank(Integer percent, Float payment, String eventId, String targetId, Support support, Project project) {
        CrowdfundRank crowdfundRank = new CrowdfundRank();
        crowdfundRank.setEventId(eventId);
        crowdfundRank.setPercent(percent);
        crowdfundRank.setProjectId(support.getProjectId());
        crowdfundRank.setTargetId(targetId);
        crowdfundRank.setPayment(payment);
        crowdfundRank.setArrivalTime(support.getUpdateDate());
        // 发起众筹的时间
        Long startTime = project.getCreateDate().getTime();
        // 阶段到达时间
        Long arrivalTime = support.getUpdateDate().getTime();
        Long diff = arrivalTime - startTime;
        crowdfundRank.setDiffTime(diff > 0 ? diff : 0);
        this.insert(crowdfundRank);
    }

    @Override
    public void doCrowdRank(int afterPercent, int beforePercent, Activity activity, TargetProject targetProject, Support support, Project project) {
        int newAfterPercent = afterPercent > 10 ? 10 : afterPercent;
        // 保存众筹排名
        if (newAfterPercent > beforePercent) {
            this.insertCrowdRank(newAfterPercent, project.getTargetAmount(), activity.getEventId(), targetProject.getTargetId(), support, project);
        }

        int result = newAfterPercent - beforePercent;
        if (result > 1) {
            for (int i = 1; i < result; i++) {
                this.insertCrowdRank(beforePercent + i, project.getTargetAmount(), activity.getEventId(), targetProject.getTargetId(), support, project);
            }
        }
    }

    @Override
    public void deleteByProjectId(String projectId) {
        crowdfundRankWriteDao.deleteByProjectId(projectId);
    }
}
